package com.stlm2.controller;

import com.stlm2.core.common.page.PageView;
import com.stlm2.dems.entity.*;
import com.stlm2.dems.service.FluxService;
import com.stlm2.dems.service.UserService;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.math.NumberUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import sun.text.resources.cldr.fr.FormatData_fr_LU;

import javax.annotation.Resource;
import java.io.Flushable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Author：fuchunyu
 * Date：2018/1/2
 * Description：
 */
@Controller
@RequestMapping("flux")
public class FluxController {

    @Resource
    private UserService userService;
    @Resource
    private FluxService fluxService;

    //积分商品列表页
    @RequestMapping("/list")
    public String list() {
        return "/flux/list";
    }

    /*
* crm添加商品页面
* */
    @RequestMapping(value = "/addView", produces = "application/json;charset=UTF-8")
    public ModelAndView addView(ModelAndView modelAndView) {
        modelAndView.setViewName("/flux/add");
        return modelAndView;
    }

    /*
* crm商品详情页面
* */
    @RequestMapping(value = "/detailsView", produces = "application/json;charset=UTF-8")
    public ModelAndView detailsView(ModelAndView modelAndView, String id) {
        modelAndView.setViewName("/flux/details");
        modelAndView.addObject("id", id);
        return modelAndView;
    }


    /**
     * @param page  页数
     * @param token 用户标识
     * @param state 商品状态
     * @return 积分商品查询
     */
    @ResponseBody
    @RequestMapping(value = "/search.json", produces = "application/json;charset=utf-8")
    public String search(ModelMap model, String page, String token, Integer state) {

        int pageNo = 1;
        int pageSize = 20;
        if (NumberUtils.isDigits(page) && !"0".equals(page.trim())) {
            pageNo = NumberUtils.toInt(page);
        }
        if (StringUtils.isEmpty(token)) {
            return CodeContent.returnLogin();
        }
        User loginUser = userService.getByToken(token);
        if (loginUser == null) {
            return CodeContent.returnLogin();
        }
        Flux flux=new Flux();
        if(!StringUtils.isEmpty(state))
            flux.setState(1);
        PageView pageview = fluxService.getPage(pageNo, pageSize, flux);
        if (pageview != null) {
            model.put("datas", fluxService.convertModel(pageview.getRecords()));
            model.put("recordsTotal", pageview.getRowCount());
            model.put("totalPage", pageview.getPageCount());
            model.put("pageNow", pageview.getPageNow());
            model.put("code", 1);
            model.put("msg", "success");
        }
        return new JSONObject(model).toString();
    }

    /**
     *
     * @param state
     * @param flux
     * @param money
     * @param token
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/add.json", produces = "application/json;charset=utf-8")
    public String add(Integer state,Integer flux,BigDecimal money,String token) {

        User user = userService.getByToken(token);
        if (user == null)
            return CodeContent.returnLogin();

        if (StringUtils.isEmpty(flux))
            return CodeContent.returnFailure("流量不能为空！");
        if (StringUtils.isEmpty(money))
            return CodeContent.returnFailure("金额不能为空！");
        if (StringUtils.isEmpty(state))
            return CodeContent.returnFailure("商品状态不能为空！");
        Flux flux1=new Flux();
        flux1.setState(1);
        flux1.setCreateTime(new Date());
        flux1.setCreateUserId(user.getId());
        flux1.setFlux(flux);
        flux1.setMoney(money);

        fluxService.addEntity(flux1);
        return CodeContent.returnSuccess(null);
    }

    /**
     *
     * @param id
     * @param state
     * @param flux
     * @param money
     * @param token
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update.json", produces = "application/json;charset=utf-8")
    public String update(Integer id,Integer state,Integer flux,BigDecimal money,String token) {
        User user = userService.getByToken(token);
        if (user == null)
            return CodeContent.returnLogin();
        Flux flux1 = fluxService.findEntityById(id);
        if (flux1 == null)
            return CodeContent.returnFailure("没有找到记录！");
        if (!StringUtils.isEmpty(state))
            flux1.setState(state);
        if (!StringUtils.isEmpty(flux))
            flux1.setFlux(flux);
        if (!StringUtils.isEmpty(money))
            flux1.setMoney(money);

        fluxService.updateEntity(flux1);
        return CodeContent.returnSuccess(null);
    }


    /**
     * @param id    商品ID
     * @param token 用户标识
     * @return 订单详情
     */
    @ResponseBody
    @RequestMapping(value = "/detail.json", produces = "application/json;charset=utf-8")
    public String detail(Integer id, String token) {
        User user = userService.getByToken(token);
        if (user == null)
            return CodeContent.returnLogin();
        Flux flux = fluxService.findEntityById(id);
        if (flux == null)
            return CodeContent.returnFailure("订单查询失败！");
        ModelMap modelMap = fluxService.buildModel(flux);
        return CodeContent.returnSuccess(modelMap);
    }
}
